﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DBBase;
using System.Security;
using System.Security.Cryptography;

using System.Text.RegularExpressions;
namespace Business
{
    public partial class AddUsers : Form
    {
        public MainForm m_mainform;

        public AddUsers()
        {
            InitializeComponent();
        }
        private string Encryption(string express)//加密
        {
            CspParameters param = new CspParameters();
            param.KeyContainerName = "gzwk2006";
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
            {
                byte[] plaindate = Encoding.Default.GetBytes(express);
                byte[] encryptdate = rsa.Encrypt(plaindate, false);

                return Convert.ToBase64String(encryptdate);
            }
        }
        //
        private string Decrypt(string cip)//解密
        {
            CspParameters param = new CspParameters();
            param.KeyContainerName = "gzwk2006";
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
            {
                byte[] encryptdate = Convert.FromBase64String(cip);
                byte[] decryptdate = rsa.Decrypt(encryptdate, false);
                return Encoding.Default.GetString(decryptdate);
            }
        }
        
        public static bool IsInt(string value)
        {
            return Regex.IsMatch(value, @"^[+ -]?\d*$");
        }
        private void text_sfzh_Validating(object sender, CancelEventArgs e)
        {
            if ((!Regex.IsMatch(text_sfzh.Text, @"^(^\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$", RegexOptions.IgnoreCase)))
            {
                MessageBox.Show("身份证号格式不正确！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;

            }
        }
        public void cheip(string sipdz)
        {
            if (sipdz == "")
            {
                //MessageBox.Show("IP地址不能为空");
                return;

            }
            else
            {
                // 是否包含三个点
                string sfzh = sipdz.Replace(".", "");
                if ((sipdz.Length - sfzh.Length) == 3)
                {
                    string ipnr1 = sipdz.Split(new char[] { '.' })[0];
                    if (!IsInt(ipnr1))
                    {
                        MessageBox.Show("IP地址格式不正确");
                        return;
                    }
                    else
                    {
                        if (int.Parse(ipnr1) > 255)
                        {
                            MessageBox.Show("IP地址格式不正确");
                            return;
                        }
                    }
                    string ipnr2 = sipdz.Split(new char[] { '.' })[1];
                    if (!IsInt(ipnr2))
                    {
                        MessageBox.Show("IP地址格式不正确");
                        return;
                    }
                    else
                    {
                        if (int.Parse(ipnr2) > 255)
                        {
                            MessageBox.Show("IP地址格式不正确");
                            return;
                        }
                    }
                    string ipnr3 = sipdz.Split(new char[] { '.' })[2];
                    if (!IsInt(ipnr3))
                    {
                        MessageBox.Show("IP地址格式不正确");
                        return;
                    }
                    else
                    {
                        if (int.Parse(ipnr3) > 255)
                        {
                            MessageBox.Show("IP地址格式不正确");
                            return;
                        }
                    }
                    string ipnr4 = sipdz.Split(new char[] { '.' })[3];
                    if (!IsInt(ipnr4))
                    {
                        MessageBox.Show("IP地址格式不正确");
                        return;
                    }
                    else
                    {
                        if (int.Parse(ipnr4) > 255)
                        {
                            MessageBox.Show("IP地址格式不正确");
                            return;
                        }
                    }
                }
                else
                {
                    MessageBox.Show("IP地址格式不正确");
                    return;
                }


            }
        }

        private void text_GDIP1_Validating(object sender, CancelEventArgs e)
        {
            cheip(text_GDIP1.Text);
        }

        private void text_GDIP2_Validating(object sender, CancelEventArgs e)
        {
            cheip(text_GDIP2.Text);
        }

        private void text_GDIP3_Validating(object sender, CancelEventArgs e)
        {
            cheip(text_GDIP3.Text);
        }

        private void text_IPQS_Validating(object sender, CancelEventArgs e)
        {
            cheip(text_IPQS.Text);
        }

        private void text_IPZZ_Validating(object sender, CancelEventArgs e)
        {
            cheip(text_IPZZ.Text);
        }

        private void but_query_Click(object sender, EventArgs e)
        {
            if (text_zhm.Text == "")
            {
                MessageBox.Show("帐户名为空不允许添加");
                return;
            }
            if (text_zsxm.Text == "")
            {
                MessageBox.Show("姓名为空不允许添加");
                return;
            }
            if (text_sfzh.Text == "")
            {
                MessageBox.Show("身份证号为空不允许添加");
                return;
            }
            if (text_IPQS.Text == "")
            {
                MessageBox.Show("起始IP地址为空不允许添加");
                return;
            }
            if (text_IPZZ.Text == "")
            {
                MessageBox.Show("终止IP地址为空不允许添加");
                return;
            }
            if (text_GDIP1.Text == "")
            {
                MessageBox.Show("固定登录IP地址为空不允许添加");
                return;
            }
            
            Query objquery = new Query();
            string ssql = "";
            ssql = string.Format(@"Select count(*) From USERE where SFZHM='{0}'", text_sfzh.Text);
            if (objquery.ExecuteS(ssql))
            {
                MessageBox.Show(string.Format("该身份证号已存在{0}", text_sfzh.Text));
                return;
            }

            ssql = string.Format(@"Select count(*) From USERE where GZRYBH='{0}'", text_gzrybh.Text);
            if (objquery.ExecuteS(ssql))
            {
                MessageBox.Show(string.Format("警员编号已存在{0}", text_gzrybh.Text));
                return;
            }
            ssql = string.Format(@"Select count(*) From USERE where NAME='{0}'", text_zsxm.Text);
            if (objquery.ExecuteS(ssql))
            {
                MessageBox.Show(string.Format("真实姓名{0}已存在", text_zsxm.Text));
                return;
            }

            ssql = string.Format(@"Select count(*) From USERE where YHLB='{0}'", text_zhm.Text);
            if (objquery.ExecuteS(ssql))
            {
                MessageBox.Show(string.Format("已经存在账户名{0}", text_zhm.Text));
                return;
            }
            else
            {
                ssql = string.Format(@"insert into USERE (
                                    YHJS,
                                    PASSWORD,
                                    YHLB,
                                    NAME,
                                    SFZHM,
                                    GZRYBH,
                                    BZ,
                                    MMYXQ,
                                    YHYXQ,
                                    IPXY,
                                    MACXY,
                                    IPQS,
                                    IPJS,
                                    GDIP1,
                                    GDIP2,
                                    GDIP3,
                                    MAC,
                                    QSSJ,
                                    JSSJ,
                                    YHSDSJ,
                                    ZJDLSJ,
                                    ZHMJM) values 
                                    ('{0}',
                                    '{1}',
                                    '{2}',
                                    '{3}',
                                    '{4}',
                                    '{5}',
                                    '{6}',
                                    '{7}',
                                    '{8}',
                                    '{9}',
                                    '{10}',
                                    '{11}',
                                    '{12}',
                                    '{13}',
                                    '{14}',
                                    '{15}',
                                    '{16}',
                                    '{17}',
                                    '{18}',
                                    '{19}',
                                    '{20}',
                                    '{21}')", com_yhlb.Text,
                                    Encryption("admin_888888"),
                                    text_zhm.Text,
                                    text_zsxm.Text,
                                    text_sfzh.Text,
                                    text_gzrybh.Text,
                                    text_bz.Text,
                                    dateTimePicker1.Value.ToString("yyyy-MM-dd  HH:mm:ss"),
                                    dateTimePicker2.Value.ToString("yyyy-MM-dd  HH:mm:ss"),
                                    check_IPXY.Checked ? "有" : "无",
                                    check_MACXY.Checked ? "有" : "无",
                                    text_IPQS.Text,
                                    text_IPZZ.Text,
                                    text_GDIP1.Text,
                                    text_GDIP2.Text,
                                    text_GDIP3.Text,
                                    text_MACDZ.Text,
                                    dateTimePicker3.Value.ToString("HH:mm:ss"),
                                    dateTimePicker4.Value.ToString("HH:mm:ss"),
                                    DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                    DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                    Encryption(text_zhm.Text));
                if (!objquery.UpdateTable(ssql))
                {
                    MessageBox.Show("添加失败");
                }
                else
                {
                    MessageBox.Show("用户新增成功");
                }
            }
            //判断是否核心功能
            if (m_mainform.shxgn.Contains("用户管理增加"))
            {
                m_mainform.baocunhxrj("用户管理增加", string.Format("增加新用户{0}:{1}", com_yhlb.Text, "insert into USERE"), "写入");// 核心功能日记  日记内容
            }
            //保存操作日记
            m_mainform.savecaozuoriji(ssql, "写入", "系统管理", "用户管理增加");
        }

        private void but_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddUsers_Load(object sender, EventArgs e)
        {
            //判断是否民警
            if (m_mainform.sfjy.Contains("用户管理增加"))
            {
                if (!m_mainform.SFMJ.Contains("是"))
                {
                    //but_query.Enabled = false;
                }
            }
        }

       
    }
}
